package com.winderinfo.yikaotianxia.ui.shoppingcart;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;
import com.winderinfo.yikaotianxia.ui.xiangqin.DetailsActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShoppingCartActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.sh_num)
    TextView shNum;
    @BindView(R.id.sh_recycler)
    RecyclerView shRecycler;
    @BindView(R.id.sh_che)
    CheckBox shChe;
    @BindView(R.id.ch_price)
    TextView chPrice;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;
    private ShoppingCartAdapter shoppingCartAdapter;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_shoppingcar;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        shRecycler.setLayoutManager(new LinearLayoutManager(this));
        spUtils = new SPUtils(this);
    }

    @Override
    protected void initData() {
        //请求购物车数据
        /**
         * @userId 用户id
         * */
        String userID = spUtils.getString("UserID", null);
        map.clear();
        map.put("id", 1);
        presenter.onPostRequest(Api.SHOPPING, map, ShoppingcartBean.class);
    }

    @Override
    public void onSuccess(Object data) {
        //购物车数据
        if (data instanceof ShoppingcartBean) {
            List<ShoppingcartBean.YkCartListBean> ykCartList = ((ShoppingcartBean) data).getYkCartList();
            shNum.setText("共 " + ykCartList.size() + " 门课程");
            shoppingCartAdapter = new ShoppingCartAdapter(this, ykCartList);
            shRecycler.setAdapter(shoppingCartAdapter);
            //选择框
            shoppingCartAdapter.setOnwisCheck(new ShoppingCartAdapter.OnwisCheck() {
                @Override
                public void getIsCheck(boolean ischeck) {
                    shChe.setChecked(ischeck);
                }
            });
            //价格
            shoppingCartAdapter.setOnPrice(new ShoppingCartAdapter.OnPrice() {
                @Override
                public void getId(double price) {
                    chPrice.setText("￥ " + price);
                }
            });
            //商品详情
            shoppingCartAdapter.setOnId(new ShoppingCartAdapter.OnId() {
                @Override
                public void getOnId(int id, String type) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    bundle.putString("type", type);
                    openActivity(DetailsActivity.class, bundle);
                }
            });
            //删除
            shoppingCartAdapter.setOndel(new ShoppingCartAdapter.Ondel() {
                @Override
                public void getDetId(int id) {
                    /**
                     * @ids 购物车Id
                     * */
                    map.clear();
                    map.put("ids", id);
                    presenter.onPostRequest(Api.REMOVE, map, RegisterBean.class);
                }
            });
        }
        //删除
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, "删除成功");
                String userID = spUtils.getString("UserID", null);
                map.clear();
                map.put("id", 1);
                presenter.onPostRequest(Api.SHOPPING, map, ShoppingcartBean.class);
            }else {
                ToastUtil.showSuccess(this, "删除失败");
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }


    @OnClick({R.id.sh_che, R.id.sh_zhifu, R.id.sh_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sh_che://全选全不选
                if (shChe.isChecked()) {
                    shoppingCartAdapter.setIsCheck(true);
                } else {
                    shoppingCartAdapter.setIsCheck(false);
                }
                break;
            case R.id.sh_zhifu:
                break;
            case R.id.sh_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

}
